<?php
declare (strict_types=1);

namespace app\api\controller\v5;

use app\common\models\Datum;
use app\common\models\DatumCate;
use app\common\models\Error;
use app\common\models\Paper;
use app\common\models\Question;

class ErrorLog extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'book' => $this->book(),   //课本名称
            'list' => $this->getList(),     //  获取学习记录信息
            'lists' => $this->getLists(),     //  获取错题信息
        ]);
    }

    //左侧名称
    public function book()
    {
        $type = input('type/d', 1);
        //学习记录
        if ($type == 1) {
            $arr = Error::where(['status' => 1, 'api_member_id' => $this->user_id])->group('datum_cate_id')->column('datum_cate_id');
//            $data = Datum::where('id', 'in', $arr)->where('status', 1)->column('datum_cate_id');
            $paper = DatumCate::where('id', 'in', $arr)->where('status', 1)->field('id,title')->select();
        } elseif ($type == 2) {
            //错题
            $arr = Error::where(['status' => 1, 'api_member_id' => $this->user_id])->column('question_id');
            $data = Question::where('id', 'in', $arr)->where('status', 1)->column('paper_id');
            $paper = Paper::where('id', 'in', $data)->where('status', 1)->field('id,title')->select();
        }
        return $paper;
    }

    /*
 * 获取学习记录信息
 * */
    public function getList()
    {
        $type = 1;
        $datum_cate_id = input('datum_cate_id/d');
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 15);
        $model = new Error();
        //$datum_ids = Datum::where('datum_cate_id', $datum_cate_id)->where('status', 1)->column('id');
        $list = $model->where(['type' => $type, 'api_member_id' => $this->user_id])
            ->where('datum_cate_id', $datum_cate_id)
            ->where('status', 1)
            ->page($page, $pageSize)
            ->select()
            ->toArray();
        foreach ($list as &$v) {
            $v['video'] = config("app.domain_url") . $v['video'];
        }
        return $list;
    }

    /*
     * 获取错误信息
     * */
    public function getLists()
    {
        $paper_id = input('paper_id/d');
        $type = 2;
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 15);
        $model = new Error();
        $question_ids = Question::where('paper_id', $paper_id)->where('status', 1)->column('id');
        $list = $model->where(['type' => $type, 'api_member_id' => $this->user_id])
            ->where('question_id', 'in', $question_ids)
            ->where('status', 1)
            ->page($page, $pageSize)
            ->select()
            ->toArray();
        foreach ($list as &$data) {
            if ($data['content'])
                $data['content'] = json_decode($data['content']);
        }
        return $list;
    }
}
